package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/service"
)

type cProject struct{}

var Project = cProject{}

func (p *cProject) GetProjectPageList(ctx context.Context, req *v1.GetProjectPageListReq) (res *v1.GetProjectPageListRes, err error) {
	total, list, err := service.Project().ListByConditionWithPage(ctx, model.PageInput(req.PageCommonReq), model.ProjectPageListConditionInput{
		Title: req.Title,
	})
	if err != nil {
		return
	}
	res = &v1.GetProjectPageListRes{
		Total: total,
		List:  list,
	}
	return
}

func (p *cProject) CreateProject(ctx context.Context, req *v1.CreateProjectReq) (res *v1.Res, err error) {
	_, err = service.Project().Save(ctx, model.EditProjectInput(req.EditProjectReq))
	return
}

func (p *cProject) ModifyProject(ctx context.Context, req *v1.ModifyProjectReq) (res *v1.Res, err error) {
	err = service.Project().Update(ctx, req.Id, model.EditProjectInput(req.EditProjectReq))
	return
}

func (p *cProject) GetProjectDetail(ctx context.Context, req *v1.GetProjectDetailReq) (res *v1.GetProjectDetailRes, err error) {
	pro, err := service.Project().GetById(ctx, req.Id)
	if err != nil {
		return
	}
	res = &v1.GetProjectDetailRes{Project: pro}
	return
}

func (p *cProject) DeleteProject(ctx context.Context, req *v1.DeleteProjectReq) (res *v1.Res, err error) {
	err = service.Project().SoftDelete(ctx, req.Ids)
	return
}

func (p *cProject) ProjectAttachResume(ctx context.Context, req *v1.ProjectAttachResumeReq) (res *v1.Res, err error) {
	err = service.Project().AttachResume(ctx, req.ProjectId, req.Ids)
	return
}

func (p *cProject) ProjectUnAttachResume(ctx context.Context, req *v1.ProjectUnAttachResumeReq) (res *v1.Res, err error) {
	err = service.Project().UnAttachResume(ctx, req.ProjectId, req.Id)
	return
}

func (p *cProject) GetProjectResume(ctx context.Context, req *v1.GetProjectResumeReq) (res *v1.GetProjectResumeRes, err error) {
	total, list, err := service.Project().ProjectResumeListByConditionWithPage(ctx, req.ProjectId, model.PageInput(req.PageCommonReq), model.ResumePageListCondition{
		Username: req.Username,
		Phone:    req.Phone,
	})
	if err == nil {
		res = &v1.GetProjectResumeRes{
			Total: total,
			List:  list,
		}
	}
	return
}
